package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.stadium.api.req.ReqCategory;
import com.quanyan.stadium.api.req.ReqLocation;
import com.quanyan.stadium.api.req.ReqPKGameRankList;
import com.quanyan.stadium.entity.vo.UserGameCategory;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.StadiumIndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author dongshenglin
 * @date 2016/7/12  11:46
 */

@RestController
@RequestMapping("/api/stadium/index")
public class StadiumIndexController {

    private final static Logger logger = LoggerFactory.getLogger(StadiumIndexController.class);

    @Autowired
    private StadiumIndexService stadiumIndexService;


    @Autowired
    private BaseService baseService;

    /**
     *
     *  运动场首页用户数据
     */
    @RequestMapping(value = "/userInfo",method = RequestMethod.POST)
    public APIResponse<UserGameCategory> getUserGradeIntegral(@RequestBody @Valid ReqCategory reqCategory, HttpServletRequest request){
        try {
            return stadiumIndexService.getUserGradeIntegral((Integer) request.getAttribute("uid"), reqCategory.getGameCategoryId());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("userInfo调用失败 e:", e);
            return APIResponse.returnFail("Server exception");
        }
    }

    /**
     *
     *  运动场首页,俱乐部活动展示
     */
    @RequestMapping(value = "/clubActivityDisplay",method = RequestMethod.POST)
    public APIResponse<?> clubActivityDisplay(@RequestBody @Valid ReqLocation reqLocation, BindingResult result, HttpServletRequest request){
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        //lat lng 兼容器
        if (null == reqLocation.getLat() || null == reqLocation.getLng()) {
            return APIResponse.returnSuccess(PageObj.create(0, 0, 20, null));
        }
        return baseService.queryClubRecommendActivity((Integer) request.getAttribute("uid"), reqLocation);
    }

    /**
     * PK排行榜表单信息展示
     *
     * */
    @RequestMapping(value = "/queryPKGameRankList",method = RequestMethod.POST)
    public APIResponse<?> queryPKGameRankList(@RequestBody @Valid ReqPKGameRankList reqPKGameRankList,  HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        return APIResponse.returnSuccess(stadiumIndexService.queryPKGameRankListNotCache(reqPKGameRankList, (Integer) request.getAttribute("uid")));
    }
    

}
